<?php

namespace LaraPress\Config;

use ReduxFramework;

class LaraPressApi
{
    /**
     * Redux 实例
     *
     * @var ReduxFramework
     */
    public $redux;

    /**
     * 配置数组
     *
     * @var array
     */
    public $args = array();

    /**
     * 配置选项
     *
     * @var array
     */
    public $sections = array();

    /**
     * 构造函数
     */
    public function __construct()
    {
        if (!class_exists('ReduxFramework')) {
            return;
        }

        $this->init();
    }

    /**
     * 初始化 Redux 配置
     */
    public function init()
    {

        $this->set_args();
        $this->set_sections();
        $this->redux = new ReduxFramework($this->sections, $this->args);
    }

    /**
     * 设置 Redux 参数
     */
    private function set_args()
    {
        $this->args = array(
            'opt_name'                  => 'wp_lara_press_options',
            'display_name'              => 'Larapress 设置',
            'display_version'           => WP_LARA_PRESS_VERSION,
            'menu_type'                 => 'menu',
            'allow_sub_menu'            => true,
            'menu_title'                => 'Larapress 设置',
            'page_title'                => 'Larapress 设置',
            'async_typography'          => true,
            'admin_bar'                 => false,
            'admin_bar_icon'            => 'dashicons-portfolio',
            'admin_bar_priority'        => 50,
            'global_variable'           => 'wp_lara_press_options',
            'dev_mode'                  => false,
            'update_notice'             => true,
            'customizer'                => false,
            'page_priority'             => null,
            'page_parent'               => '',
            'page_permissions'          => 'manage_options',
            'menu_icon'                 => 'dashicons-database',
            'last_tab'                  => '',
            'page_icon'                 => 'icon-themes',
            'page_slug'                 => 'wp-lara-press-options',
            'save_defaults'             => true,
            'default_show'              => false,
            'default_mark'              => '*',
            'show_import_export'        => true,
            'transient_time'            => 60 * MINUTE_IN_SECONDS,
            'output'                    => true,
            'output_tag'                => true,
            'database'                  => '',
            'use_cdn'                   => true,
            'language'                  => 'zh_CN',
            'hints'                     => array(
                'icon'          => 'el el-question-sign',
                'icon_position' => 'right',
                'icon_color'    => 'lightgray',
                'icon_size'     => 'normal',
                'tip_style'     => array(
                    'color'   => 'light',
                    'shadow'  => true,
                    'rounded' => false,
                    'style'   => '',
                ),
                'tip_position'  => array(
                    'my' => 'top left',
                    'at' => 'bottom right',
                ),
                'tip_effect'    => array(
                    'show' => array(
                        'effect'   => 'slide',
                        'duration' => '500',
                        'event'    => 'mouseover',
                    ),
                    'hide' => array(
                        'effect'   => 'slide',
                        'duration' => '500',
                        'event'    => 'click mouseleave',
                    ),
                ),
            ),
            'show_options_object'       => false,
        );
    }

    /**
     * 设置配置选项
     */
    private function set_sections()
    {
        $this->sections = array(
            array(
                'title'  => 'API 设置',
                'id'     => 'api_settings',
                'desc'   => '配置 Laravel API 相关设置',
                'icon'   => 'el el-cog',
                'fields' => array(
                    array(
                        'id'       => 'api_enabled',
                        'type'     => 'switch',
                        'title'    => '启用 API',
                        'subtitle' => '启用或禁用 Laravel API 功能',
                        'default'  => true,
                    ),
                    array(
                        'id'       => 'api_prefix',
                        'type'     => 'text',
                        'title'    => 'API 前缀',
                        'subtitle' => 'API 路径前缀',
                        'default'  => 'larapress-api',
                        'required' => array('api_enabled', '=', true),
                    ),
                    array(
                        'id'       => 'api_cors_enabled',
                        'type'     => 'switch',
                        'title'    => '启用 CORS',
                        'subtitle' => '允许跨域请求',
                        'default'  => true,
                        'required' => array('api_enabled', '=', true),
                    ),
                    array(
                        'id'       => 'api_cors_origins',
                        'type'     => 'textarea',
                        'title'    => 'CORS 允许的域名',
                        'subtitle' => '每行一个域名',
                        'default'  => '*',
                        'required' => array('api_cors_enabled', '=', true),
                    ),
                ),
            ),
            array(
                'title'  => '缓存设置',
                'id'     => 'cache_settings',
                'desc'   => '配置缓存相关设置',
                'icon'   => 'el el-time',
                'fields' => array(
                    array(
                        'id'       => 'cache_enabled',
                        'type'     => 'switch',
                        'title'    => '启用缓存',
                        'subtitle' => '启用 API 响应缓存',
                        'default'  => true,
                    ),
                    array(
                        'id'       => 'cache_duration',
                        'type'     => 'spinner',
                        'title'    => '缓存时间（分钟）',
                        'subtitle' => 'API 响应缓存持续时间',
                        'default'  => 60,
                        'min'      => 1,
                        'max'      => 1440,
                        'step'     => 1,
                        'required' => array('cache_enabled', '=', true),
                    ),
                ),
            ),
        );
    }
}
